package com.zhq.blog.core.domain;

import java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@SuppressWarnings("rawtypes")
public class Page<T> implements org.springframework.data.domain.Page<T>{
	public static final int PAGE_NUMBER_COUNT = 9;
	public static final int MIDDLE_COUNT = 4;
	private org.springframework.data.domain.Page springPage;
	
	private List<Integer> pageNumberList;
	
	public Page(org.springframework.data.domain.Page springPage) {
		super();
		this.springPage = springPage;
		
		pageNumberList = new java.util.LinkedList<Integer>();


		for(int index = getNumber() - MIDDLE_COUNT;index < getNumber(); index++){
			if(index>=0)pageNumberList.add(index);
		}
		pageNumberList.add(getNumber());
		for(int index = getNumber() +1 ;index <getNumber() +1 + MIDDLE_COUNT; index++){
			if(index < getTotalPages()){
				pageNumberList.add(index);
			}
		}
		if(pageNumberList.size() < PAGE_NUMBER_COUNT){
			if(pageNumberList.get(pageNumberList.size() - 1) == getTotalPages() -1 ){
				
				for(int index = pageNumberList.get(0) -1;index >=0 && pageNumberList.size() <PAGE_NUMBER_COUNT;index--){
					pageNumberList.add(0,index);
				}
			}
			if(pageNumberList.indexOf(0) == 0){
				for(int index = pageNumberList.get(pageNumberList.size() - 1) + 1;index < getTotalPages() && pageNumberList.size() <PAGE_NUMBER_COUNT ;index++){
					pageNumberList.add(index);
				}
			}		
		}
	}
	
	public Page(org.springframework.data.domain.Page springPage,int page_number_count) {
		super();
		this.springPage = springPage;
		
		pageNumberList = new java.util.LinkedList<Integer>();


		for(int index = getNumber() - MIDDLE_COUNT;index < getNumber(); index++){
			if(index>=0)pageNumberList.add(index);
		}
		pageNumberList.add(getNumber());
		for(int index = getNumber() +1 ;index <getNumber() +1 + MIDDLE_COUNT; index++){
			if(index < getTotalPages()){
				pageNumberList.add(index);
			}
		}
		if(pageNumberList.size() < page_number_count){
			if(pageNumberList.get(pageNumberList.size() - 1) == getTotalPages() -1 ){
				
				for(int index = pageNumberList.get(0) -1;index >=0 && pageNumberList.size() <page_number_count;index--){
					pageNumberList.add(0,index);
				}
			}
			if(pageNumberList.indexOf(0) == 0){
				for(int index = pageNumberList.get(pageNumberList.size() - 1) + 1;index < getTotalPages() && pageNumberList.size() <page_number_count ;index++){
					pageNumberList.add(index);
				}
			}		
		}
	}

	public List<Integer> getPageNumberList() {
		return pageNumberList;
	}

	public void setPageNumberList(List<Integer> pageNumberList) {
		this.pageNumberList = pageNumberList;
	}

	public org.springframework.data.domain.Page getSpringPage() {
		return springPage;
	}

	public void setSpringPage(org.springframework.data.domain.Page springPage) {
		this.springPage = springPage;
	}

	@Override
	public int getNumber() {

		return springPage.getNumber();
	}

	@Override
	public int getSize() {

		return springPage.getSize();
	}

	@Override
	public int getTotalPages() {

		return springPage.getTotalPages();
	}

	@Override
	public int getNumberOfElements() {

		return springPage.getNumberOfElements();
	}

	@Override
	public long getTotalElements() {
		
		return springPage.getTotalElements();
	}
	
	@Override
	public Pageable nextPageable() {
		
		return springPage.nextPageable();
	}

	@Override
	public Pageable previousPageable() {
		
		return springPage.previousPageable();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Iterator<T> iterator() {
		
		return springPage.iterator();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> getContent() {
		
		return springPage.getContent();
	}

	@Override
	public boolean hasContent() {
		
		return springPage.hasContent();
	}

	@Override
	public Sort getSort() {
		
		return springPage.getSort();
	}

	@Override
	public boolean isFirst() {
		return springPage.isFirst();
	}

	@Override
	public boolean isLast() {
		return springPage.isLast();
	}

	@Override
	public boolean hasNext() {
		return springPage.hasNext();
	}

	@Override
	public boolean hasPrevious() {
		return springPage.hasPrevious();
	}

	@Override
	public boolean hasPreviousPage() {
		return false;
	}

	@Override
	public boolean isFirstPage() {
		return false;
	}

	@Override
	public boolean hasNextPage() {
		return false;
	}

	@Override
	public boolean isLastPage() {
		return false;
	}
	
}
